package trocabi.view;

import trocabi.TROCABI;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import trocabi.model.Article;
import javax.swing.JTable;

public class JTableModelVenteArticles extends AbstractTableModel {

    private ArrayList<Article> articles;
    private String[] sEntetes = {"", "Référence", "Date de dépôt", "Situation", "Prix de vente", 
                                 "Type", "Aspect", "Pour", "Couleur", "Date de changement d'état"};
    
    public JTableModelVenteArticles() {
        super();
        articles = new <Article>ArrayList();
    }

    @Override
    public int getRowCount() {
        return articles.size();
    }

    @Override
    public int getColumnCount() {
        return sEntetes.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return sEntetes[columnIndex];
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        if (getColumnName(col).equals("")) {
            return true;
        }
        return false;
    }

    @Override
    public Object getValueAt(int row, int column) {
        switch (column) {
            case 0:
                return "Supprimer";
            case 1:
                return articles.get(row).getId();
            case 2:
                return TROCABI.sdfFormat_Date.format(articles.get(row).getDateDepot());
            case 3:
                return articles.get(row).getSituation();
            case 4:
                return TROCABI.dfFormat_Prix.format(Double.valueOf((articles.get(row).getPrixAchat() * (1 + articles.get(row).getTxMarge())))) + " €";
            case 5:
                return articles.get(row).getCodeType();
            case 6:
                return articles.get(row).getCodeAspect();
            case 7:
                return articles.get(row).getCodeGenre();
            case 8:
                return articles.get(row).getCouleur();
            case 9:
                return (articles.get(row).getDateChangementEtat() == null) ? "Pas de changement d'état" : TROCABI.sdfFormat_Date.format(articles.get(row).getDateChangementEtat());
            default:
                return null;
        }
    }

    public Article getRow(int iRow, JTable tTableau) {
        if (tTableau == null) {
            return articles.get(iRow);
        } else {
            return articles.get(tTableau.getRowSorter().convertRowIndexToModel(iRow));
        }
    }

    public ArrayList<Article> getArticles() {
        return articles;
    }
    public void add(Article atArticle) {
        articles.add(atArticle);
        fireTableDataChanged();
    }

    public void add(ArrayList<Article> listeArticles) {
        articles = listeArticles;
        fireTableDataChanged();
    }

    public void remove(Article article) {
        articles.remove(article);
        fireTableDataChanged();
    }

    public void remove(int row) {
        articles.remove(row);
        fireTableDataChanged();
    }
    
    public void removeAll() {
        articles = new <Article>ArrayList();
        fireTableDataChanged();
    }
    
    public void refresh() {
        fireTableDataChanged();
    }
}
